package com.wln.pms.pms.conf;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {



    public static String getMD5Str(String str) {
        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest  = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //16是表示转换为16进制数
        String md5Str = new BigInteger(1, digest).toString(16);
        return md5Str;
    }
    //加盐加密
    public static String md5Salt(String s,String salt){
        return getMD5Str(s+salt);
    }
    //加盐加次数加密
    /**
      1       md5Salt(s); = f(1)
      2       md5Salt(md5Salt(s))= f(f(1))
      3       md5Salt(md5Salt(md5Salt(s))) = f(f(2))
     */
    public static String md5Ci(String s,int num,String salt){
        //递归写法
        if(num ==1){
            return md5Salt(s,salt);
        }
        return md5Salt(md5Ci(s,num-1,salt),salt);
    }


}
